//
//  MLBLEManager.h
//  MLBLEStatic
//
//  Created by 程杰 on 2023/10/18.
//

#import <Foundation/Foundation.h>
#import "MLBLEModel.h"
NS_ASSUME_NONNULL_BEGIN

// ap配网wifiname+pass
extern NSString * const ML_AP_CODE_TYPE;
/// 发送数据code
extern NSString * const ML_SEND_CODE_TYPE;
// 获取设备信息code
extern NSString * const ML_GET_DEVICE_INFO_CODE_TYPE;
/// 纯蓝牙配网
extern NSString * const ML_BLE_CONFIG_CODE_TYPE;


// 连接设备回调，成功与否、设备信息及错误信息
typedef void(^BluetoothConnectCompletion)(BOOL success, CBPeripheral *peripheral, NSError *error);
// 断开设备回调，成功与否、设备信息及错误信息
typedef void(^BluetoothDisconnectCompletion)(BOOL success, CBPeripheral *peripheral, NSError *error);
// 数据更新回调，设备信息、特征值及错误信息
typedef void(^BluetoothDidUpdateValueForCharacteristicCompletion)(CBPeripheral *peripheral,CBCharacteristic *characteristic, NSError *error);

/// 蓝牙信息model，扫描完成后的回调，返回设备模型数组
typedef void (^BleScanCompletion )(NSArray<MLBLEModel *> *modes);
// 结果返回，返回蓝牙结果信息
typedef void (^BleResultBlock)(MLBleResultInfos *infos);
// 通知是否打开成功，返回布尔值
typedef void (^BleNotifyBlock)(BOOL isSuccess);

@interface MLBLEManager : NSObject

@property (nonatomic, copy) BluetoothConnectCompletion connectCompletion; // 连接回调属性
@property (nonatomic, copy) BluetoothDisconnectCompletion disconnectCompletion; // 断开回调属性

// 单例方法，获取共享管理器实例
+ (instancetype)sharedManager; 

/// 开始扫描蓝牙设备
- (void)mlBleScan;
/// 停止扫描并清除数据
- (void)mlBleStopScan;

/// 扫描蓝牙设备列表，支持过滤，完成后通过回调返回设备列表
/// @param isFilter 是否过滤非妙联设备，yes只显示妙联设备，no不过滤
- (void)scanPeripheralsWithFilter:(BOOL)isFilter completion:(BleScanCompletion)completion;

/// 开启心跳默认10秒
- (void)startHeartbeatTimerForPeripheral:(CBPeripheral *)peripheral;
// 关闭心跳
- (void)stopHeartbeatTimer;

// 连接到指定的蓝牙设备，连接结果通过回调返回
- (void)connectToPeripheral:(CBPeripheral *)peripheral callback:(BluetoothConnectCompletion)callback;
// 设置通知是否成功的回调
- (void)setNotifyisSuccess:(BleNotifyBlock)block;
// 设置蓝牙通信参数及结果回调
- (void)setParamsInfo:(MLBleParamsItem *)params result:(BleResultBlock)resultCallBack;
// 监听蓝牙断开连接的回调
- (void)listenBleDisConnect:(BluetoothDisconnectCompletion)disConnectBlock;
// 主动断开与蓝牙设备的连接
- (void)disconnectFromPeripheral:(CBPeripheral *)peripheral;
// 取消与蓝牙设备的连接
- (void)cancelConnectFromPeripheral:(CBPeripheral *)peripheral;
// 监听接收到的数据
- (void)listenBleReceiveDataResult:(BleResultBlock)resultCallBack;

@end

NS_ASSUME_NONNULL_END
